package dgut.haitaobao.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import dgut.haitaobao.HaitaobaoApplication;
import dgut.haitaobao.service.ProductParamsService;
import dgut.haitaobao.util.response.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/productParams")
@CrossOrigin
public class ProductParamsController {
    @Autowired
    private ProductParamsService productParamsService;
    @RequestMapping("/getBrands")
    public HttpResponse getBrandsByCategoryId(@RequestBody String categoryId) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(categoryId);
        String categoryIdText = jsonNode.get("categoryId").asText();
        return productParamsService.getBrandsByCategoryId(categoryIdText);
    }

    @RequestMapping("/getBrandsByProductName")
    public HttpResponse getBrandsByproductName(@RequestBody String productName) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(productName);
        String productNameText = jsonNode.get("productName").asText();
        return productParamsService.getBrandsByProductName(productNameText);
    }
}
